<template>
    <el-card class="box-card">
        <div slot="header" class="clearfix">
            <span>账号登录</span>
        </div>
        <div>
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
                <el-form-item label="用户账号" prop="userNumber">
                    <el-input v-model="ruleForm.userNumber"></el-input>
                </el-form-item>
                <el-form-item label="用户密码" prop="passwd">
                    <el-input v-model="ruleForm.passwd" type="password"></el-input>
                </el-form-item>
                <el-form-item label="身份选择" prop="role">
                    <el-select v-model="ruleForm.role" placeholder="请选择用户身份">
                        <el-option label="学生" value="students"></el-option>
                        <el-option label="教师" value="teachers"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
    </el-card>
</template>

<script>
import api from '@/api'
export default {
    data() {
        var userNumberRegex = (rule, value, callback) => {
            let userNumberRegex = /^20[0-9]{2}[0-9]{5}[0-9][0-9][1-9]$/;
            if (userNumberRegex.test(value)) {
                callback();
            } else {
                callback(new Error('请输入正确的工号/学号'));

            }
        };
        var passwdRegex = (rule, value, callback) => {
            let passwdRegex = /^[a-zA-Z0-9]{6,18}$/;
            if (passwdRegex.test(value)) {
                callback();
            } else {
                callback(new Error('请输入正确的密码'));

            }
        };
        return {
            ruleForm: {
                userNumber: "",
                passwd: "",
                role: "students"
            },
            rules: {
                userNumber: [
                    { required: true, message: '请输入工号/学号', trigger: 'blur' },
                    { validator: userNumberRegex, trigger: 'blur' }
                ],
                passwd: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    { validator: passwdRegex, trigger: 'blur' }
                ]
            }
        }
    },
    methods: {
        async login() {
            let res = await api.login({ ...this.ruleForm })
            if (res.code == 200) {
                this.$notify({
                    title: '成功',
                    message: res.message,
                    type: 'success'
                });
                localStorage.token = res.token
                this.$router.replace("/home")
            } else {

                if (!res.unJoin) {
                    this.$notify({
                        title: '警告',
                        message: res.message,
                        type: 'warning'
                    });
                } else {
                    this.$notify.error({
                        title: '错误',
                        dangerouslyUseHTMLString: true,
                        message: `<p>${res.message}</p><p>拒绝原因:${res.unJoin}</p>`
                    });
                }

            }
        },
        submitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.login()
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    }
}
</script>

<style lang="scss" scoped>
.box-card {
    width: 500px;
    position: absolute;
    top: 30%;
    left: 50%;
    transform: translate(-50%, -50%)
}
</style>